package com.heima.search.config;

import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApTheadLocalUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shitou
 * @className WmTokenInterceptor
 * @description: TODO
 */
@Slf4j
@Data
public class ApTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取用户信息
        String userId = request.getHeader("userId");
        // id 存入线程
        if (StringUtils.isNotBlank(userId)) {
            ApUser apUser = new ApUser();
            apUser.setId(Integer.parseInt(userId));
            ApTheadLocalUtil.setApUser(apUser);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        ApTheadLocalUtil.clearApUser();
    }
}
